// 设置保存路径和文件名
const multer = require("multer");
const path = require("path");
const writeLogs = require('./writeLogs');

const storage = multer.diskStorage({
    destination: function(req, res, cb) {
        // 设置文件存储路径
        cb(null, path.join(__dirname, '../', '../', 'avatars'))
    },
    filename: function(req, file, cb) {
        // 设置文件名， 该操作可以解码中文字符防止乱码
        const username = req.auth.username;
        cb(null, username + '.png');
    }
})

const filter = function (req, file, cb) {
    const reg = /^image\/png$/;
    if (reg.test(file.mimetype)) cb(null, true);
    else cb(null, false);
}

const limits = {
    fieldSize: '1MB',
}

exports.multerModule = multer({
    storage: storage,
    fileFilter: filter,
    limits: limits,
})

exports.reqHandler = (req, res) => {
    writeLogs('edit', {
        username: req.auth.username,
        info: '头像',
    })
    res.cc('ok',0)
}